package xyz.lne.researchgroupmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import xyz.lne.researchgroupmanage.controller.request.AddDocumentRequest;
import xyz.lne.researchgroupmanage.controller.response.DocumentResponse;
import xyz.lne.researchgroupmanage.entity.Document;
import xyz.lne.researchgroupmanage.entity.Research;
import xyz.lne.researchgroupmanage.exception.BadRequestException;
import xyz.lne.researchgroupmanage.mapper.DocumentMapper;
import xyz.lne.researchgroupmanage.service.DocumentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.lne.researchgroupmanage.service.dataTransferObject.DocumentDTO;
import xyz.lne.researchgroupmanage.service.dataTransferObject.DocumentSimpleDTO;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author team7
 * @since 2021-01-19
 */
@Service
@Transactional
public class DocumentServiceImpl extends ServiceImpl<DocumentMapper, Document> implements DocumentService {

    @Autowired
    private DocumentMapper documentMapper;

    @Override
    public List<DocumentSimpleDTO> findDocumentSimpleByResearchId(Integer researchId) {
        List<DocumentSimpleDTO> documentSimpleDTOS = new LinkedList<>();

        QueryWrapper<Document> documentQueryWrapper = new QueryWrapper<>();
        documentQueryWrapper.eq("research_id", researchId);
        List<Document> documents = documentMapper.selectList(documentQueryWrapper);

        Set<String> name = new HashSet<>();
        documents.forEach(document -> {
            if (!name.contains(document.getName())) {
                DocumentSimpleDTO documentSimpleDTO = new DocumentSimpleDTO();
                BeanUtils.copyProperties(document, documentSimpleDTO);
                documentSimpleDTO.setDocumentName(document.getName());
                documentSimpleDTOS.add(documentSimpleDTO);
                name.add(document.getName());
            }
        });
        return documentSimpleDTOS;
    }

    @Override
    public DocumentDTO findDocumentByDocumentId(Integer documentId) {
        DocumentDTO documentDTO = new DocumentDTO();
        Document document = documentMapper.selectById(documentId);
        if (document == null) {
            Map<String, Object> e = new HashMap<>();
            e.put("该文档不存在", documentId);
            throw new BadRequestException(e);
        }
        BeanUtils.copyProperties(document, documentDTO);
        documentDTO.setDocumentName(document.getName());
        return documentDTO;
    }

    @Override
    public List<DocumentSimpleDTO> findDocumentByDocumentName(Integer researchId, String documentName) {
        List<DocumentSimpleDTO> documentSimpleDTOS = new LinkedList<>();

        QueryWrapper<Document> documentQueryWrapper = new QueryWrapper<>();
        documentQueryWrapper.eq("name", documentName)
                .eq("research_id", researchId);
        List<Document> documents = documentMapper.selectList(documentQueryWrapper);

        if (documents == null) {
            Map<String, Object> e = new HashMap<>();
            e.put("该文档不存在", documentName);
            throw new BadRequestException(e);
        }

        documents.forEach(document -> {
            DocumentSimpleDTO documentSimpleDTO = new DocumentSimpleDTO();
            BeanUtils.copyProperties(document, documentSimpleDTO);
            documentSimpleDTO.setDocumentName(document.getName());
            documentSimpleDTOS.add(documentSimpleDTO);
        });
        return documentSimpleDTOS;
    }

    @Override
    public DocumentDTO findDocumentByVersion(Integer researchId, String documentName, Integer version) {
        DocumentDTO documentDTO = new DocumentDTO();

        QueryWrapper<Document>documentQueryWrapper = new QueryWrapper<>();
        documentQueryWrapper.eq("name", documentName)
                            .eq("research_id", researchId)
                            .eq("version", version);

        Document document = documentMapper.selectOne(documentQueryWrapper);
        if(document == null) {
            Map<String, Object> e = new HashMap<>();
            e.put("该文档不存在", documentName);
            throw new BadRequestException(e);
        }

        BeanUtils.copyProperties(document, documentDTO);
        documentDTO.setDocumentName(document.getName());

        return documentDTO;
    }

    @Override
    public DocumentDTO addDocument(Integer researchId, String documentName, String content, Integer userId, String remark, Integer fileId, Integer version) {
        Document document = new Document();
        document.setContent(content);
        document.setCreateTime(new Date());
        document.setFileId(fileId);
        document.setName(documentName);
        document.setRemark(remark);
        document.setResearchId(researchId);
        document.setUserId(userId);
        document.setVersion(version);

        if (documentMapper.insert(document) == 0) {
            Map<String, Object> e = new HashMap<>();
            e.put("插入文档失败", documentName);
            throw new BadRequestException(e);
        }

        DocumentDTO documentDTO = new DocumentDTO();

        BeanUtils.copyProperties(document, documentDTO);
        documentDTO.setDocumentName(document.getName());
        return documentDTO;
    }

    @Override
    public Integer deleteDocument(Integer researchId, String documentName) {
        List<DocumentSimpleDTO> documentSimpleDTOS = new LinkedList<>();

        QueryWrapper<Document> documentQueryWrapper = new QueryWrapper<>();
        documentQueryWrapper.eq("name", documentName)
                .eq("research_id", researchId);
        List<Document> documents = documentMapper.selectList(documentQueryWrapper);

        if (documents == null) {
            Map<String, Object> e = new HashMap<>();
            e.put("该文档不存在", documentName);
            throw new BadRequestException(e);
        }

        return documentMapper.delete(documentQueryWrapper);
    }

    @Override
    public Boolean deleteDocumentversion(Integer researchId, String documentName, Integer version) {
        QueryWrapper<Document> documentQueryWrapper = new QueryWrapper<>();
        documentQueryWrapper.eq("research_id", researchId)
                .eq("name", documentName)
                .eq("name", documentName)
                .eq("version", version);

        if (documentMapper.selectOne(documentQueryWrapper) == null){
            Map<String, Object> e = new HashMap<>();
            e.put("该文档不存在", documentName);
            throw new BadRequestException(e);
        }

        if (documentMapper.delete(documentQueryWrapper) == 0) {
            return false;
        }

        return true;
    }

    @Override
    public String version(Integer version) {
        return new String("V"+version+".0");
    }

    @Override
    public Integer getNewestversion(Integer researchId, String documentName) {
        QueryWrapper<Document> documentQueryWrapper = new QueryWrapper<>();
        documentQueryWrapper.eq("research_id", researchId)
                .eq("name", documentName);

        if (documentMapper.selectList(documentQueryWrapper) == null){
            Map<String, Object> e = new HashMap<>();
            e.put("该文档不存在", documentName);
            throw new BadRequestException(e);
        }

        return documentMapper.selectMaxVersion(researchId, documentName);
    }
}
